﻿using System;
using System.Linq;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Data.Services.Client;

// Author: Miroslav Matula m.matula@alutam.cz 
// Date : 16.8.2010

namespace Silverlight.Test
{
  [TestClass]
  public class ODataSourceViewTest : Microsoft.Silverlight.Testing.SilverlightTest
  {
    public ODataSourceViewTest()
    {
      _proxy = new CustomService.CustomContext(new Uri(_uristr));
      _proxy.MergeOption = MergeOption.AppendOnly;
      _dsv = new Slma.OData.ODataSourceView(_proxy,"Products");
      _dsv.LoadSize = 25;
      _dsv.PageSize = 10;
    }
    private const string _uristr = @"http://localhost:53738/Samples/CustomService.svc";
    private CustomService.CustomContext _proxy;
    Slma.OData.ODataSourceView _dsv;


    [TestMethod]
    [Asynchronous]
    public void MoveToPage01_FirstPage()
    {
      using(_dsv.DeferRefresh()) {
        _dsv.SortDescriptions.Clear();
        _dsv.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));
      }
      _dsv.MoveToFirstPage();
      if(_dsv.IsBusy) {
        _dsv.DataLoadComplete = () => {
          _dsv.DataLoadComplete = null;
          Assert.AreEqual(_dsv.Count, 10);
          TestComplete();
        };
      } else {
        Assert.Fail("Očekávám LoadData ");
        TestComplete();
      }
    }
    //------------------------------------------
    [TestMethod]
    [Asynchronous]
    public void MoveToPage02_NextPage()
    {
      _dsv.MoveToNextPage();
      if(_dsv.IsBusy) {
        Assert.Fail("Nemá být LoadData, data mají být v bufru");
        TestComplete();
      } else {
        Assert.AreEqual(_dsv.Count, 10);
        TestComplete();
      }
    }
    //------------------------------------------
    [TestMethod]
    [Asynchronous]
    public void MoveToPage03_NextPage()
    {
      _dsv.MoveToNextPage();
      if(_dsv.IsBusy) {
        _dsv.DataLoadComplete = () => {
          _dsv.DataLoadComplete = null;
          Assert.AreEqual(_dsv.Count, 10);
          TestComplete();
        };
      } else {
        Assert.Fail("Má být LoadData ");
        TestComplete();
      }
    }
    //------------------------------------------
    [TestMethod]
    [Asynchronous]
    public void MoveToPage04_PreviousPage()
    {
      _dsv.MoveToPreviousPage();
      if(_dsv.IsBusy) {
        _dsv.DataLoadComplete = () => {
          _dsv.DataLoadComplete = null;
          Assert.AreEqual(_dsv.Count, 10);
          TestComplete();
        };
      } else {
        Assert.Fail("Má být LoadData ");
        TestComplete();
      }
    }
    //------------------------------------------
    [TestMethod]
    [Asynchronous]
    public void MoveToPage05_PreviousPage()
    {
      _dsv.MoveToPreviousPage();
      if(_dsv.IsBusy) {
        _dsv.DataLoadComplete = () => {
          _dsv.DataLoadComplete = null;
          Assert.Fail("Nemá být LoadData, data mají být v bufru");
          TestComplete();
        };
      } else {
        Assert.AreEqual(_dsv.Count, 10);
        TestComplete();
      }
    }

  }
}